package com.example.app.ui.Discover;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.app.R;
import java.util.ArrayList;
import java.util.List;
public class PrivacyActivity extends AppCompatActivity {

    private RecyclerView rvOptions;
    private PrivacyAdapter adapter;
    private String selectedOption = "公开";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy);

        setupToolbar();
        setupOptionsList();
    }
    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("谁可以看");
    }
    private void setupOptionsList() {
        rvOptions = findViewById(R.id.rv_options);
        rvOptions.setLayoutManager(new LinearLayoutManager(this));
        List<PrivacyOption> options = new ArrayList<>();
        options.add(new PrivacyOption("公开", "所有朋友可见", true));
        options.add(new PrivacyOption("私密", "仅自己可见", false));
        options.add(new PrivacyOption("部分可见", "选中的朋友可见", false));
        options.add(new PrivacyOption("不给谁看", "选中的朋友不可见", false));

        adapter = new PrivacyAdapter(options);
        rvOptions.setAdapter(adapter);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            returnResult();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        returnResult();
    }
    private void returnResult() {
        Intent result = new Intent();
        result.putExtra("privacy", selectedOption);
        setResult(RESULT_OK, result);
        finish();
    }
    private class PrivacyAdapter extends RecyclerView.Adapter<PrivacyViewHolder> {
        private final List<PrivacyOption> options;
        PrivacyAdapter(List<PrivacyOption> options) {
            this.options = options;
        }
        @Override
        public PrivacyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_privacy_option, parent, false);
            return new PrivacyViewHolder(view);
        }
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void onBindViewHolder(PrivacyViewHolder holder, int position) {
            PrivacyOption option = options.get(position);
            holder.tvTitle.setText(option.title);
            holder.tvSubtitle.setText(option.subtitle);
            holder.ivCheck.setVisibility(option.isSelected ? View.VISIBLE : View.INVISIBLE);
            holder.itemView.setOnClickListener(v -> {
                selectedOption = option.title;
                for (PrivacyOption o : options) {
                    o.isSelected = o.title.equals(selectedOption);
                }
                notifyDataSetChanged();
            });
        }
        @Override
        public int getItemCount() {
            return options.size();
        }
    }
    static class PrivacyViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvSubtitle;
        ImageView ivCheck;
        PrivacyViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvSubtitle = itemView.findViewById(R.id.tv_subtitle);
            ivCheck = itemView.findViewById(R.id.iv_check);
        }
    }
    static class PrivacyOption {
        String title;
        String subtitle;
        boolean isSelected;
        PrivacyOption(String title, String subtitle, boolean isSelected) {
            this.title = title;
            this.subtitle = subtitle;
            this.isSelected = isSelected;
        }
    }
}